<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Promise</title>
</head>
<body>
    <script>
        function MyPromise(fn){
            this.state = 'pending'; // pending success error
            var _this = this;
            this.successFns = [];
            this.errorFns = [];
            fn.call(null, this.reslove);
            
        }
        MyPromise.prototype.reslove = function(val){
            // console.log(this.successFns);
            this.successFns.forEach(successFn => {
                // console.log(successFn)
                successFn.call(null, val);
            });
        }
        MyPromise.prototype.then = function(fn){
            this.successFns.push(fn);
            return this;
        }

        function a(){
            return new MyPromise(function(reslove, reject){
                reslove('success');
            })
        }
        // console.log(a())
        a().then(
            res => {
                console.log(res);
            }
        )
    </script>
</body>
</html>